/**
 * Application configuration
 */
import type { ProjectConfig } from '/#/config';

import { PROJ_CFG_KEY } from '/@/enums/cacheEnum';
// import { MenuModeEnum, MenuTypeEnum } from '/@/enums/menuEnum';
import projectSetting from '/@/settings/projectSetting';

import { updateHeaderBgColor, updateSidebarBgColor } from '/@/logics/theme/updateBackground';
import { updateColorWeak } from '/@/logics/theme/updateColorWeak';
import { updateGrayMode } from '/@/logics/theme/updateGrayMode';
import { updateDarkTheme } from '/@/logics/theme/dark';
import { changeTheme } from '/@/logics/theme';

import { useAppStore } from '/@/store/modules/app';
import { useLocaleStore } from '/@/store/modules/locale';

import { getCommonStoragePrefix, getStorageShortName } from '/@/utils/env';

import { primaryColor } from '../../build/config/themeConfig';
import { Persistent } from '/@/utils/cache/persistent';
import { deepMerge } from '/@/utils';
import { ThemeEnum } from '/@/enums/appEnum';

// Initial project configuration
export function initAppConfigStore() {
  const localeStore = useLocaleStore();
  const appStore = useAppStore();
  let projCfg: ProjectConfig = Persistent.getLocal(PROJ_CFG_KEY) as ProjectConfig;
  // let projCfg: ProjectConfig ={
  //   "showSettingButton": true,
  //   "showDarkModeToggle": true,
  //   "settingButtonPosition": "auto",
  //   "permissionMode": "ROUTE_MAPPING",
  //   "permissionCacheType": 1,
  //   "sessionTimeoutProcessing": 0,
  //   "themeColor": "#0960bd",
  //   "grayMode": false,
  //   "colorWeak": false,
  //   "fullContent": false,
  //   "contentMode": "full",
  //   "showLogo": true,
  //   "showFooter": false,
  //   "headerSetting": {
  //     "bgColor": "#ffffff",
  //     "fixed": true,
  //     "show": true,
  //     "theme": "light",
  //     "useLockPage": true,
  //     "showFullScreen": true,
  //     "showDoc": true,
  //     "showNotice": true,
  //     "showSearch": true
  //   },
  //   "menuSetting": {
  //     "bgColor": "#001529",
  //     "fixed": true,
  //     "collapsed": false,
  //     "siderHidden": false,
  //     "collapsedShowTitle": false,
  //     "canDrag": false,
  //     "show": true,
  //     "hidden": false,
  //     "menuWidth": 210,
  //     "mode":MenuModeEnum.INLINE,
  //     "type": MenuTypeEnum.MIX,
  //     "theme": "dark",
  //     "topMenuAlign": "center",
  //     "trigger": "HEADER",
  //     "accordion": true,
  //     "closeMixSidebarOnChange": false,
  //     "mixSideTrigger": "click",
  //     "mixSideFixed": false,
  //     "split": false
  //   },
  //   "multiTabsSetting": {
  //     "cache": false,
  //     "show": true,
  //     "canDrag": true,
  //     "showQuick": true,
  //     "showRedo": true,
  //     "showFold": true
  //   },
  //   "transitionSetting": {
  //     "enable": true,
  //     "basicTransition": "fade-slide",
  //     "openPageLoading": true,
  //     "openNProgress": false
  //   },
  //   "openKeepAlive": true,
  //   "lockTime": 0,
  //   "showBreadCrumb": true,
  //   "showBreadCrumbIcon": false,
  //   "useErrorHandle": false,
  //   "useOpenBackTop": true,
  //   "canEmbedIFramePage": true,
  //   "closeMessageOnSwitch": true,
  //   "removeAllHttpPending": false
  // } as ProjectConfig;
  projCfg = deepMerge(projectSetting, projCfg || {});
  const darkMode = appStore.getDarkMode;
  const {
    colorWeak,
    grayMode,
    themeColor,

    headerSetting: { bgColor: headerBgColor } = {},
    menuSetting: { bgColor } = {},
  } = projCfg;
  try {
    if (themeColor && themeColor !== primaryColor) {
      changeTheme(themeColor);
    }

    grayMode && updateGrayMode(grayMode);
    colorWeak && updateColorWeak(colorWeak);
  } catch (error) {
    console.log(error);
  }
  appStore.setProjectConfig(projCfg);

  // init dark mode
  updateDarkTheme(darkMode);
  if (darkMode === ThemeEnum.DARK) {
    updateHeaderBgColor();
    updateSidebarBgColor();
  } else {
    headerBgColor && updateHeaderBgColor(headerBgColor);
    bgColor && updateSidebarBgColor(bgColor);
  }
  // init store
  localeStore.initLocale();

  setTimeout(() => {
    clearObsoleteStorage();
  }, 16);
}

/**
 * As the version continues to iterate, there will be more and more cache keys stored in localStorage.
 * This method is used to delete useless keys
 */
export function clearObsoleteStorage() {
  const commonPrefix = getCommonStoragePrefix();
  const shortPrefix = getStorageShortName();

  [localStorage, sessionStorage].forEach((item: Storage) => {
    Object.keys(item).forEach((key) => {
      if (key && key.startsWith(commonPrefix) && !key.startsWith(shortPrefix)) {
        item.removeItem(key);
      }
    });
  });
}
